<template>
  <div>
    <div class="bg_log">
      <img src="@/assets/img/bg_log in@2x.png" alt="" />
    </div>
    <div class="info_cell">
      <img src="@/assets/img/icon_phone number@2x.png" alt="" />
      <input placeholder="请输入手机号" v-model="phone" maxlength="11" type="number" />
      <div class="del">
        <img src="@/assets/img/delete@2x.png" alt="" v-if="phone.length > 0" @click="emptyPhone" />
      </div>
    </div>
    <div class="info_cell info_cell_code">
      <img src="@/assets/img/icon_captcha@2x.png" alt="" />
      <input placeholder="请输入验证码" v-model="code" maxlength="6" @input="changeauthCd" type="number"
        style="width: calc(100% - 8.9375rem)" />
      <div class="del">
        <img src="@/assets/img/delete@2x.png" alt="" v-if="code.length > 0" @click="emptyCode" />
      </div>

      <span @click="getCode" v-if="sending == false" class="span1">获取验证码</span>
      <span v-else class="span2">重新发送({{ time }})</span>
    </div>

    <div class="registerBtn" @click="next">下一步</div>
  </div>
</template>
<script>
import { sendPhoneCode, login } from "@/api/api";
import { setToken } from "@/api/auth";
export default {
  data() {
    return {
      phone: "",
      code: "",
      sending: false,
      time: 60,
      myreg: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
    };
  },
  methods: {
    getCode() {
      if (!this.myreg.test(this.phone)) {
        alert("手机号格式不正确！")
        return
      }
      const data = {
        phone: this.phone,
        type: "CODE",
      };
      sendPhoneCode(data).then((res) => {
        if (res.code == 200) {
          this.$vux.toast.show("发送成功");
          this.sending = true;
          this.countDown();
        }
      });
    },
    // 倒计时方法
    countDown() {
      if (this.time === 0) {
        this.sending = false;
        this.time = 60;
        return;
      } else {
        this.sending = true;
        this.time--;
      }
      setTimeout(() => {
        this.countDown();
      }, 1000);
    },
    emptyPhone() {
      this.phone = "";
    },

    emptyCode() {
      this.code = "";
    },
    changeauthCd() {
      if (this.code.length > 6) {
        this.code = this.code.slice(0, 6)
      }
    },
    next() {
      const data = {
        phone: this.phone,
        code: this.code,
      };
      login(data).then((res) => {
        if (res.code == 200) {
          this.$vux.toast.show("验证成功");
          setToken(res.data.token);
          window.localStorage.setItem("phone", this.phone);
          this.$router.push({ path: "/forgetpwdnext" });
        }
      });
    },
  },
};
</script>
<style scoped>
.bg_log {
  width: 100%;
  height: 9.375rem;
  margin-bottom: 1.8125rem;
}

.bg_log img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.info_cell {
  display: flex;
  align-items: center;
  width: calc(100% - 3.75rem);
  padding: 1.125rem 0px;
  margin: 0px 1.875rem;
  border-bottom: 0.0625rem solid #e7ecf5;
}

.info_cell_code {
  justify-content: space-between;
}

.info_cell_code span {
  width: 5.125rem;
  height: 1.875rem;
  border: 1px solid rgba(87, 154, 255, 1);
  border-radius: 0.25rem;
  font-size: 0.8125rem;
  margin-left: 0.75rem;
  color: #579aff;
  line-height: 1.875rem;
  text-align: center;
}

.info_cell_code .span2 {
  border: 1px solid #949fb4;
  color: #949fb4;
}

.info_cell input {
  border: none;
  width: calc(100% - 3.0625rem);
  padding: 5px 0px;
  font-size: 0.9375rem;
  outline: none;
}

.info_cell img:nth-of-type(1) {
  display: block;
  width: 0.9375rem;
  height: 1.25rem;
  margin-right: 15px;
  object-fit: contain;
}

.info_cell .del {
  width: 1.1875rem;
  height: 1.1875rem;
}

.info_cell .del>img {
  display: block;
  width: 1.1875rem;
  height: 1.1875rem;
}

.registerBtn {
  width: 14.375rem;
  height: 2.75rem;
  background: linear-gradient(90deg,
      rgba(68, 142, 255, 1) 0%,
      rgba(102, 163, 255, 1) 100%);
  border-radius: 0.1875rem;
  text-align: center;
  line-height: 2.75rem;
  color: #fff;
  font-size: 1.125rem;
  margin: 0px auto;
  margin-top: 4.3125rem;
}
</style>